package com.zsl.swing.redis.desktop.window;

import com.zsl.swing.redis.desktop.common.Constants;
import com.zsl.swing.redis.desktop.common.IconPaths;
import com.zsl.swing.redis.desktop.model.DbCountConfigEntity;
import com.zsl.swing.redis.desktop.model.FontSizeConfigEntity;
import com.zsl.swing.redis.desktop.model.OtherGeneralConfigEntity;
import com.zsl.swing.redis.desktop.utils.CommonUtils;
import com.zsl.swing.redis.desktop.utils.ConfigUtils;
import com.zsl.swing.redis.desktop.utils.FontUtils;
import com.zsl.swing.redis.desktop.utils.IconUtils;
import com.zsl.swing.redis.desktop.window.menu.ZslMenuBar;
import com.zsl.swing.redis.desktop.window.panel.ZslConnectionPanel;
import com.zsl.swing.redis.desktop.window.panel.ZslLogPanel;
import com.zsl.swing.redis.desktop.window.panel.ZslShowPanel;
import com.zsl.swing.redis.desktop.window.panel.ZslSplitPane;
import com.zsl.swing.redis.desktop.window.tree.ZslServerTree;

import javax.swing.*;
import java.awt.*;

/**
 * 
 * @author 张帅令
 * {@code @description}  程序主窗口
 *
 */
public class ZslRedisDesktopMainWindow extends JFrame{

	private static final long serialVersionUID = 1L;

	/**
	 * 加载字体大小设置
	 */
	private final FontSizeConfigEntity fontSizeConfig = ConfigUtils.readFontSizeConfig();

	/**
	 * 加载数据库展示数量设置
	 */
	private final DbCountConfigEntity dbCountConfig = ConfigUtils.readDbCountConfig();

	/**
	 * 其他通用配置
	 */
	private final OtherGeneralConfigEntity otherGeneralConfig = ConfigUtils.readOtherGeneralConfig();

	/**
	 * 菜单栏
	 */
	private ZslMenuBar zslMenuBar /*= new ZslMenuBar()*/;

	/**
	 * 连接面版
	 */
	private ZslConnectionPanel zslConnectionPanel/* = new ZslConnectionPanel()*/;

	/**
	 * 错误日志面版
	 */
	private ZslLogPanel zslLogPanel/* = new ZslLogPanel()*/;

	/**
	 * 数据展示面版
	 */
	private ZslShowPanel zslShowPanel/* = new ZslShowPanel()*/;

	private static ZslRedisDesktopMainWindow mainWindow;

	private ZslRedisDesktopMainWindow() {
		super();
		this.setTitle("RedisGUI");
		this.setIconImage(IconUtils.getScaleImage(IconPaths.DESKTOP_ICON,Constants.ICON_WIDTH,Constants.ICON_HEIGHT));

		mainWindow = this;

		this.initChildrenComponents();

		//设置窗口大小
		this.setSize(Constants.MAIN_WINDOW_WIDTH, Constants.MAIN_WINDOW_HEIGHT);
		this.setFont(FontUtils.getFont(fontSizeConfig.getFontName(), Font.BOLD,fontSizeConfig.getWindow()));

		//计算窗口位置
		int x = CommonUtils.maxWidth()/2 - Constants.MAIN_WINDOW_WIDTH /2;
		int y = CommonUtils.maxHeight()/2 - Constants.MAIN_WINDOW_HEIGHT /2;
		this.setLocation(x,y);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//设置菜单
		this.setJMenuBar(zslMenuBar);

		JSplitPane right = new ZslSplitPane(JSplitPane.VERTICAL_SPLIT,Constants.MAIN_WINDOW_DIVIDER_SIZE,zslShowPanel, zslLogPanel);
		right.setDividerLocation(Constants.MAIN_WINDOW_HEIGHT *6/8);

		//连接面版
		JSplitPane mainSplitPane = new ZslSplitPane(JSplitPane.HORIZONTAL_SPLIT, Constants.MAIN_WINDOW_DIVIDER_SIZE, zslConnectionPanel, right);
		mainSplitPane.setDividerLocation(Constants.MAIN_WINDOW_WIDTH /7);

		mainSplitPane.setVisible(true);

		this.setContentPane(mainSplitPane);

		this.setVisible(true);
		

		
	}

	private void initChildrenComponents() {
		this.zslMenuBar = new ZslMenuBar();
		this.zslLogPanel = new ZslLogPanel();
		this.zslShowPanel = new ZslShowPanel();

		JTree zslServerTree = ZslServerTree.getTree();

		ZslServerTree.refreshTree();
		this.zslConnectionPanel = new ZslConnectionPanel(zslServerTree);
	}

	public static void showWindow(){
		new ZslRedisDesktopMainWindow();
	}

	public static void closeWindow(){
		mainWindow.dispose();
	}

	public static OtherGeneralConfigEntity getOtherGeneralConfig(){
		return mainWindow.otherGeneralConfig;
	}

	public static DbCountConfigEntity getDbCountConfig(){
		return mainWindow.dbCountConfig;
	}

	public static FontSizeConfigEntity getFontSizeConfig(){
		return mainWindow.fontSizeConfig;
	}

	public static ZslRedisDesktopMainWindow getMainWindow(){
		return mainWindow;
	}

	public static ZslMenuBar getZslMenuBar(){
		return mainWindow.zslMenuBar;
	}

	public static ZslConnectionPanel getZslConnectionPanel(){
		return mainWindow.zslConnectionPanel;
	}

	public static ZslLogPanel getZslLogPanel(){
		return mainWindow.zslLogPanel;
	}

	public static ZslShowPanel getZslShowPanel(){
		return mainWindow.zslShowPanel;
	}


}
